#!/bin/bash
set -e
set -x
MYDIR="${0%/*}"
cd "$MYDIR/.."
source "recipes/common.sh"


# RECIPE START gcc
#
# depends: glibc

GCC_RUNTIME_LIBS="libatomic.so libgomp.so libitm.so libstdc++.so"

# XXX: black magic ahead.
# GCC needs glibc headers, and using previously compiled gmp, mpfr,
# mpc, and isl headers breaks (the newly compiled) glibc headers.
# Therefore symlink gmp, mpfr, mpc, isl sources from the gcc source
# tree and compile those libraries again (not sure why this trick
# even works)

# Do not search in ${stage}${prefix}/include to avoid breaking
# glibc headers
save_CFLAGS="$CFLAGS"
save_CPPFLAGS="$CPPFLAGS"
save_LDFLAGS="$LDFLAGS"
export CFLAGS='-O2 -g'
unset CPPFLAGS
unset LDFLAGS
# XXX: ALT Linux' rpm-build forces every executable to be PIE, even
# on architectures where this causes a substantial performance degradation
ALT_LINUX_IDIOCY=UnfortunatelyYes

reset_mtime gcc gcc/configure.ac
rm -rf build/gcc
mkdir build/gcc
cd build/gcc
env \
	ac_cv_file__proc_self_exe=yes \
	gcc_cv_libc_provides_ssp=yes \
../../gcc/configure \
	--target="$TARGET" \
	${CONFIGURE_ARGS} \
	--enable-languages=c,c++ \
	--enable-version-specific-runtime-libs \
	--with-gcc-major-version-only \
	--disable-libsanitizer \
	--disable-multilib \
	--enable-gnu-unique-object \
	--enable-linker-build-id \
	${ALT_LINUX_IDIOCY+--enable-default-pie} \
	${NIL}

# XXX: make runs configure in subdirs. autoconf variables do NOT propagate
# from the top-level configure to sub-configures, so specify them again:
env \
	ac_cv_file__proc_self_exe=yes \
	gcc_cv_libc_provides_ssp=yes \
make -j${JOBS}
# XXX: install to $rootfs (where glibc is installed) since GCC
# seems to do some magic with headers during the installation
make -j${JOBS} install DESTDIR="$rootfs"
ln -sf gcc ${rootfs}${prefix}/bin/cc
wipe_la_files "$rootfs"
cd ../..
rm -rf build/gcc

# FIXME: limits.h is fscked up and causes compile failures.
# Copy the working headers from the cross-compiler (assumption: the cross-
# and the native compilers are built from the very same source)
xgcc_libgcc_a="`${TARGET}-gcc --print-libgcc-file-name`"
xgcc_priv_dir="${xgcc_libgcc_a%/*}"
xgcc_ver="${xgcc_priv_dir##*/}"
# XXX: assumption: the cross-compiler has been built from the very
# same source with the same/similar configuration
gcc_priv_dir="${prefix}/${target_libdir}/gcc/${TARGET}/${xgcc_ver}"

cp -a "${xgcc_priv_dir}/include/limits.h" "${rootfs}${gcc_priv_dir}/include/limits.h"
rsync -avH  --delete "${xgcc_priv_dir}/include-fixed/" "${rootfs}${gcc_priv_dir}/include-fixed/"

deploy_dso () {
	local dso_link="$1" # /path/to/libstdc++.so
	local public_libdir="$2"
	local gcc_priv_dir="${dso_link%/*}"
	local dso # /path/to/libstdc++.so.6.0.31
	local dso_ver_link # /path/to/libstdc++.so.6
	dso=`readlink -f $dso_link`
	dso_ver_link="${dso%.*}" # strip .N.M
	dso_ver_link="${dso_ver_link%.*}"
	mv -f --target-directory="$public_libdir" "$dso" "$dso_ver_link"
	ln -sfr "$public_libdir/${dso_ver_link##*/}" "$dso_link"
}

for lib in $GCC_RUNTIME_LIBS; do
	deploy_dso "${rootfs}${gcc_priv_dir}/$lib" "${rootfs}${prefix}/${target_libdir}"
done
mv -f --target-directory="${rootfs}${prefix}/${target_libdir}" "${rootfs}${gcc_priv_dir}/libgcc_s.so.1"

export CFLAGS="$save_CFLAGS"
export CPPFLAGS="$save_CPPFLAGS"
export LDFLAGS="$save_LDFLAGS"


# RECIPE END gcc
